<?php

class ExcursionController extends CController
{
	public function actionIndex()
	{
		$this->setPageTitle('Экскурсии');
		
		$records = null;
		$count   = null;
		
		$excursionTypeId = ((isset($_GET['excursionTypeId']) && intval($_GET['excursionTypeId']) != 0) ? intval($_GET['excursionTypeId']) : 0); 
		
		if ($excursionTypeId > 0) {
			$records = Excursion::model()->criteria('title', 'asc')->findAll('excursionTypeId=:excursionTypeId', array(':excursionTypeId' => $excursionTypeId));
		} else {
			$records = Excursion::model()->criteria('title', 'asc')->findAll();
		}
		
		$count = count($records);
		
		$excursionTypes = CActiveRecordData::getArray(
			ExcursionType::model()->criteria('title', 'asc')->findAll(),
			array('id', 'title'), false
		);
		
		$this->render('list', array(
			'excursionTypes' => $excursionTypes,
			'excursionTypeId' => $excursionTypeId > 0 ? $excursionTypeId : 0,
			'entity' => 'excursion',
			'data'   => $records,
			'count'  => $count,
			'detailedUrl' => '/excursion/detailed/id/' 
		));
	}
	
	public function actionDetailed()
	{
		$excursionId = intval($_GET['id']);
		
		if ($excursionId > 0) {
			$excursion = Excursion::model()->with(array('attractions'))->findByPk($excursionId);
			
			$this->setPageTitle($excursion->title);
			
			$this->render('detailed', array(
				'excursion' => $excursion
			));
		}
	}
}